<?php
/**
 * @version		$Id: invoices.php 3 2011-12-16 20:19:15Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHosterModelInvoices extends JModelList
{

	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'InvoiceId', 'a.InvoiceId',
				'InvoiceOrder', 'a.InvoiceOrder', 'order',
				'InvoiceClient', 'a.InvoiceClient', 'client',
				'InvoiceDate', 'a.InvoiceDate',
				'InvoiceDue', 'a.InvoiceDue',
				'InvoiceProduct', 'a.InvoiceProduct',
				'InvoiceAmount', 'a.InvoiceAmount',
				'InvoiceStatus', 'a.InvoiceStatus',
				'InvoiceIp', 'a.InvoiceIp',
				'InvoiceNote', 'a.InvoiceNote'
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('administrator');
		
		$filters = JRequest::getVar('filters');
		if (empty($filters)) {
			$data = $app->getUserState($this->context.'.data');
			$filters = $data['filters'];
		}
		else {
			$app->setUserState($this->context.'.data', array('filters'=>$filters));
		}
		
		$clientId = $app->getUserStateFromRequest($this->context.'.filter.client_id', 'filter_client_id');
		$this->setState('filter.client_id', $clientId);
		
		$orderId = $app->getUserStateFromRequest($this->context.'.filter.order_id', 'filter_order_id');
		$this->setState('filter.order_id', $orderId);
		
		$statusId = $app->getUserStateFromRequest($this->context.'.filter.status_id', 'filter_status_id');
		$this->setState('filter.status_id', $statusId);
		
		parent::populateState('a.InvoiceId', 'desc');		
				
	}

	
	protected function getListQuery() 
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		
		$query->select($this->getState('list.select', 'a.*'));
		
		$query->from('#__almh_invoices AS a');
		
		$query->select('c.ClientName AS client');
		$query->join('LEFT', '#__almh_clients AS c ON c.ClientId = a.InvoiceClient');
			

		$clientId = $this->getState('filter.client_id');
		if (is_numeric($clientId)) {
			$query->where('a.InvoiceClient = '.$clientId);
		}
		
		$orderId = $this->getState('filter.order_id');
		if (is_numeric($orderId)) {
			$query->where('a.InvoiceOrder = '.$orderId);
		}
		
		$statusId = $this->getState('filter.status_id');
		if (is_numeric($statusId)) {
			$query->where('a.InvoiceStatus = '.$statusId);
		}
		
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.InvoiceId')).' '.$db->getEscaped($this->getState('list.direction', 'desc')));
			
		return $query;		
	}
	
	public function getClient() {
		
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		$query->select('c.ClientId AS value, c.ClientName AS text');
		$query->from('#__almh_clients AS c');
		$query->join('INNER', '#__almh_invoices AS i ON i.InvoiceClient = c.ClientId');
		$query->group('c.ClientId');
		$query->order('c.ClientName');

		$db->setQuery($query->__toString());

		return $db->loadObjectList();
	}
}
?>